%% inout image
clc,close,clear;
addpath(genpath(pwd));
imgInput=imread('..\data\21.jpg');
imgInput=rgb2gray(imgInput); 
[yLength, xLength] = size(imgInput);
%% smooth phase
lambda = 1;
kappa = 1;
imgSmooth = imgInput;
%imgSmooth = L0Smoothing(imgInput,0.01);
%imgSmooth = tsmooth(imgInput,0.015,3);
%figure,imshow(imgSmooth),title('Smooth Result');
%% edge detection phase
thresh=[0.1,0.5];  
sigma=1.5;
imgEdge = edge(double(imgSmooth),'canny',thresh,sigma);  
%figure,imshow(imgEdge),title('Edge Detection Result');
%% line detection phase
[H, theta, rho]= hough(imgEdge,'RhoResolution', 0.3);
peak = houghpeaks(H,2);hold on;
lines = houghlines(imgEdge,theta,rho,peak);  
%figure,
imshow(imgEdge),title('Hough Transform Detect Result'),hold on;
for k=1:length(lines)
    xy=[lines(k).point1;lines(k).point2];  
    plot(xy(:,1),xy(:,2),'r-','LineWidth',2);  
end
%% angel computation phase
rangePressure = 60;
rangeDegree = 270;
coordCenter = [xLength/2,yLength/2];
coordStart = [xLength/4,yLength/4];
anglePointer = (lines(1).theta+lines(2).theta)/2;
A = [lines(1).point2(2)-lines(1).point1(2),lines(1).point1(1)-lines(1).point2(1);lines(2).point2(2)-lines(2).point1(2),lines(2).point1(1)-lines(2).point2(1)];
b = [lines(1).point1(1)*(lines(1).point2(2)-lines(1).point1(2))-lines(1).point1(2)*(lines(1).point2(1)-lines(1).point1(1)); ... 
    lines(2).point1(1)*(lines(2).point2(2)-lines(2).point1(2))-lines(2).point1(2)*(lines(2).point2(1)-lines(2).point1(1))];
intersecPoint = round(A\b)
plot(intersecPoint(1),intersecPoint(2),'g.');
pressureDisplay = 100;
figure;
imshow(imgInput); hold on;
for k=1:length(lines)
    xy=[lines(k).point1;lines(k).point2];  
    plot(xy(:,1),xy(:,2),'r-','LineWidth',2);  
    hold on;
end
plot(intersecPoint(1),intersecPoint(2),'g.');hold on;
plot(114,545,'y.');hold on;